package cn.tedu.jdbc;

//防止SQL注入
import java.sql.*;
import java.util.Scanner;

public class login1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

       Class.forName("com.mysql.cj.jdbc.Driver");
       String url = "jdbc:mysql://localhost:3306/gdb2109";
        Connection con = DriverManager.getConnection(url, "root", "root");
        System.out.println("请输入用户名：");
        String user = new Scanner(System.in).nextLine();
        System.out.println("请输入密码：");
        String pwd = new Scanner(System.in).nextLine();
        String sql = "select * from user2 where name=? and password = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1,user);
        ps.setString(2,pwd);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }

        rs.close();
        ps.close();
        con.close();

    }
}
